package com.xys.springboot_easyui_a001.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.xys.springboot_easyui_a001.entity.User;
import com.xys.springboot_easyui_a001.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.plugin2.message.SetJVMIDMessage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "hello";
    }

    @RequestMapping("/find_all")
    @ResponseBody
    public Map<String, Object> findAll(){
        Map<String, Object> map = new HashMap<String, Object>();
        List<User> users = userService.findAll();
        map.put("users", users);
        return map;
    }

    @RequestMapping("/find_page_info")
    @ResponseBody
    public JSONObject findPageInfo(
            @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(name = "rows", required = false, defaultValue = "10") Integer rows,
            @RequestParam(name = "id", required = false) Long id
    ){
        User user = new User();
        if(id != null && id > 0){
            user.setId(id);
        }
        PageInfo<User> pageInfo = userService.findPageInfo(page, rows, user);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", pageInfo.getTotal());
        map.put("rows", pageInfo.getList());
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(map));
        return jsonObject;
    }

    @RequestMapping("/go_to_user_list")
    public String goToUserList(){
        return "admin/user/user_list";
    }

    @RequestMapping("/go_to_index")
    public String goToIndex(){
        return "admin/index";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Map<String, Object> edit(User user){
        Map<String, Object> map = new HashMap<String, Object>();
        if(user == null){
            map.put("message", "参数错误");
            map.put("success", false);
            return map;
        }
        if(user.getId() != null && user.getId() > 0){
            userService.update(user);
            map.put("message", "编辑成功");
        }else{
            userService.insert(user);
            map.put("message", "新增成功");
        }
        map.put("success", true);
        return map;
    }

    @RequestMapping(value = "/deletes", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deletes(Long[]  ids){
        Map<String, Object> map = new HashMap<String, Object>();
        if(ids == null || ids.length < 1){
            map.put("message", "参数错误");
            map.put("success", false);
            return map;
        }
        userService.deletes(ids);
        map.put("message", "删除成功");
        map.put("success", true);
        return map;
    }
}
